package com.itheima.tlias.service.impl;

import com.itheima.tlias.exception.BusinessException;
import com.itheima.tlias.mapper.DeptMapper;
import com.itheima.tlias.service.DeptService;
import com.itheima.tlias.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 05 4月 2025 10:37
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    /**
     * 查询所有部门信息
     *
     * @return
     */
    @Override
    public List<Dept> findAll() {
    return deptMapper.findAll();
    }


    /**
     * 删除部门信息
     *
     * @param uid
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer uid) {
        //根据id先查询该部门下是否有员工
        List<Dept> depts = deptMapper.findByIdEmp(uid);
        if (!depts.isEmpty()) {
           throw new BusinessException("该部门下还有员工，不能删除！");
        }

        deptMapper.delete(uid);
    }

    /**
     * 添加部门信息
     *
     * @param dept
     */
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);

    }
    /**
     *  根据id查询部门信息回显
     */
    @Override
    public Dept findById(Integer uid) {
      return deptMapper.findById(uid);
    }

    /**
     * 修改部门信息
     * @param dept
     */
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

}
